
import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:ble2025_plugin_example/util/app_all_value.dart';
import 'package:ble2025_plugin_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

///天气设置
class WeatherPage extends StatefulWidget {
  const WeatherPage({Key? key}) : super(key: key);

  @override
  State<WeatherPage> createState() => _WeatherPageState();
}

class _WeatherPageState extends State<WeatherPage> {

  TextEditingController weatherIdController = TextEditingController();
  TextEditingController tempController = TextEditingController();
  TextEditingController maxTempController = TextEditingController();
  TextEditingController minTempController = TextEditingController();
  TextEditingController contentController = TextEditingController();
  AppAllValueController controller = Get.find();

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg){
      Map map = arg;
      switch (map[DeviceKey.DataType] as String) {
        case BleConst.Weather:
          controller.showContentDialog(map.toString());
          break;
      }}
    );
  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("天气设置".tr),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Container(
            width: 300,
            child: TextField(
              controller: weatherIdController,
              textAlign: TextAlign.center,
              keyboardType: TextInputType.number,
              textInputAction: TextInputAction.done,
              decoration: InputDecoration(
                hintText: '天气Id (0-38 或 99)'.tr,
              ),
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9]')),//只允许输入数
              ],
            ),
          ),
          Container(
            width: 300,
            child: TextField(
              controller: tempController,
              textAlign: TextAlign.center,
              keyboardType: TextInputType.number,
              textInputAction: TextInputAction.done,
              decoration: InputDecoration(
                hintText: '当前温度'.tr,
              ),
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9]')),//只允许输入数
              ],
            ),
          ),
          Container(
            width: 300,
            child: TextField(
              controller: maxTempController,
              textAlign: TextAlign.center,
              keyboardType: TextInputType.number,
              textInputAction: TextInputAction.done,
              decoration: InputDecoration(
                hintText: '当天最高温度'.tr,
              ),
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9]')),//只允许输入数
              ],
            ),
          ),
          Container(
            width: 300,
            child: TextField(
              controller: minTempController,
              textAlign: TextAlign.center,
              keyboardType: TextInputType.number,
              textInputAction: TextInputAction.done,
              decoration: InputDecoration(
                hintText: '当天最低温度'.tr,
              ),
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9]')),//只允许输入数
              ],
            ),
          ),
          Container(
            width: 300,
            child: TextField(
              controller: contentController,
              textAlign: TextAlign.center,
              keyboardType: TextInputType.text,
              textInputAction: TextInputAction.done,
              decoration: InputDecoration(
                hintText: '地点'.tr,
              )
            ),
          ),
          // contentController
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('设置'.tr),
                  onPressed: (){
                    if(weatherIdController.text.isEmpty)return;
                    if(tempController.text.isEmpty)return;
                    if(maxTempController.text.isEmpty)return;
                    if(minTempController.text.isEmpty)return;
                    if(contentController.text.isEmpty)return;
                      var data  = WeatherData(
                          tempNow: int.parse(tempController.text),
                          tempHigh: int.parse(maxTempController.text),
                        tempLow: int.parse(minTempController.text),
                        weatherId: int.parse(weatherIdController.text),
                        cityName: contentController.text,
                      );
                      controller.writeData(BleSDK.setWeather(data));
                  },
                ),
              )),
            ],
          ),
        ],
      ),
    );
  }
}
